<?php
namespace App\Http\Controllers;

use App\Http\Utils\PayUtil;

class PayController extends Controller {
    /**
     * 发起支付
     */
    public function pay(){
        $res=PayUtil::wxpay(time(),0.01,"openidxxxxxx",'/api/notify/wxpay/recharge','购买商品','mini');
        return show_success('请支付',$res);
    }

    /**
     * 支付回调
     */
    public function notify(string $provider,string $order_type){
        try{
            switch ($provider){
                case 'wxpay':
                    $data=Pay::wechat(config("pay"))->callback();
                    if(env("APP_DEBUG")){
                        Log::info("支付成功回调参数:".request()->path(),json_decode(json_encode($data,JSON_UNESCAPED_UNICODE),true));
                    }
                    if($data->event_type=="TRANSACTION.SUCCESS"){
                        $pay_no= $data->resource['ciphertext']['out_trade_no'];
                        $pay_money=bcdiv($data->resource['ciphertext']['amount']['total'],100,2);
                        // ......

                    }

                    break;
                case 'alipay':
                    $data=Pay::alipay(config("pay"))->callback();
                    break;
            }
        }catch(\Exception $e){
            if(env("APP_DEBUG")){
                Log::info("支付回调处理异常",[$e->getMessage()]);
            }
        }

    }
}
